package neo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.java1234.model.PageBean;
import com.java1234.util.PageUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;

import neo.model.Device;
import neo.service.DeviceService;
import neo.util.Keyword;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/device")
public class DeviceController {
	
	@Autowired
	private DeviceService deviceService;
	
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value="page",required=false)String page,Device s_device,Keyword keyword,HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		HttpSession session=request.getSession();
		if(StringUtil.isEmpty(page)){
			page="1";
			session.setAttribute("s_device_keyword", keyword);
		}else{
			keyword=(Keyword) session.getAttribute("s_device_keyword");
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),5);
		List<Device> deviceList=deviceService.find(pageBean, keyword);
		int total=deviceService.count();
		String pageCode=PageUtil.getPagation(request.getContextPath()+"/device/list.do", total, Integer.parseInt(page), 5);
		
		mav.addObject("pageCode", pageCode);
		mav.addObject("modeName", "设备列表");
		mav.addObject("deviceList", deviceList);
		mav.addObject("keyword",keyword);
		mav.addObject("mainPage", "device/list.jsp");
		mav.setViewName("main");
		return mav;
	}
	
	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value="id",required=false)String id){
		ModelAndView mav=new ModelAndView();
		//List<EquipmentType> equipmentTypeList=equipmentTypeService.find(null, null);
		mav.addObject("mainPage", "device/save.jsp");
		mav.addObject("modeName", "设备管理");
		//mav.addObject("equipmentTypeList",equipmentTypeList);
		mav.setViewName("main");
		if(StringUtil.isNotEmpty(id)){
			mav.addObject("actionName", "设备修改");
			Device device=deviceService.loadById(Integer.parseInt(id));
			mav.addObject("device", device);
		}else{
			mav.addObject("actionName", "设备添加");			
		}
		return mav;
	}
	
	@RequestMapping("/save")
	public String save(Device device){
		if(device.getId()==null){
			deviceService.add(device);
		}else{
			deviceService.update(device);
		}
		return "redirect:/device/list.do";
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
		JSONObject result=new JSONObject();
		deviceService.delete(Integer.parseInt(id));
		result.put("success", true);			
		ResponseUtil.write(result, response);
	}
}
